import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/HomeView.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/home/index',
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: '/home/index',
        name: '/home/index',
        component: () => import('../views/Index.vue'),
      },
      {
        path: '/home/actor-list',
        name: '/home/actor-list',
        component: () => import('../views/actor/ActorList.vue'),
        meta: {
          nav: [{ text: '演员管理' }, { text: '演员列表' }], // 动态生成导航
        },
      },
      {
        path: '/home/actor-add',
        name: '/home/actor-add',
        component: () => import('../views/actor/ActorAdd.vue'),
        meta: {
          nav: [{ text: '演员管理' }, { text: '新增演员' }], // 动态生成导航
        },
      },
      {
        path: '/home/director-add',
        name: '/home/director-add',
        component: () => import('../views/director/DirectorAdd.vue'),
        meta: {
          nav: [{ text: '导演管理' }, { text: '新增导演' }], // 动态生成导航
        },
      },
      {
        path: '/home/director-list',
        name: '/home/director-list',
        component: () => import('../views/director/DirectorList.vue'),
        meta: {
          nav: [{ text: '导演管理' }, { text: '导演列表' }], // 动态生成导航
        },
      },
      {
        path: '/home/movie-add',
        name: '/home/movie-add',
        component: () => import('../views/movie/MovieAdd.vue'),
        meta: {
          nav: [{ text: '电影管理' }, { text: '新增电影' }], // 动态生成导航
        },
      },
      {
        path: '/home/movie-list',
        name: '/home/movie-list',
        component: () => import('../views/movie/MovieList.vue'),
        meta: {
          nav: [{ text: '电影管理' }, { text: '电影列表' }], // 动态生成导航
        },
      },
      {
        path: '/home/movie-update/:id',
        name: '/home/movie-update',
        component: () => import('../views/movie/MovieUpdate.vue'),
        meta: {
          nav: [
            { text: '电影管理' },
            { text: '电影列表', to: '/home/movie-list' },
            { text: '修改电影信息' },
          ], // 动态生成导航
        },
      },
      {
        path: '/home/cinema-add',
        name: '/home/cinema-add',
        component: () => import('../views/cinema/CinemaAdd.vue'),
        meta: {
          nav: [{ text: '电影院管理' }, { text: '新增电影院' }], // 动态生成导航
        },
      },
      {
        path: '/home/cinema-list',
        name: '/home/cinema-list',
        component: () => import('../views/cinema/CinemaList.vue'),
        meta: {
          nav: [{ text: '电影院管理' }, { text: '电影院列表' }], // 动态生成导航
        },
      },
      {
        path: '/home/cinema-update/:id',
        name: '/home/cinema-update',
        component: () => import('../views/cinema/CinemaUpdate.vue'),
        meta: {
          nav: [
            { text: '电影院管理' },
            { text: '电影院列表', to: '/home/cinema-list' },
            { text: '修改电影院信息' },
          ], // 动态生成导航
        },
      },
      {
        path: '/home/cinema-room-list/:id',
        name: '/home/cinema-room-list',
        component: () => import('../views/cinema/cinemaRoomList.vue'),
        meta: {
          nav: [
            { text: '电影院管理' },
            { text: '电影院列表', to: '/home/cinema-list' },
            { text: '放映厅列表' },
          ], // 动态生成导航
        },
      },
      {
        path: '/home/cinema-room-list/:id/plan-add/:roomID',
        name: '/home/cinema-room-list/:id/plan-add/:roomID',
        component: () => import('../views/cinema/MoviePlanAdd.vue'),
        meta: {
          nav: [
            { text: '电影院管理' },
            { text: '电影院列表', to: '/home/cinema-list' },
            { text: '放映厅列表' },
            { text: '新增排片计划' },
          ], // 动态生成导航
        },
      },
      {
        path: '/home/cinema-room-list/:id/plan-list/:room_id',
        name: '/home/cinema-room-list/:id/plan-list/:room_id',
        component: () => import('../views/cinema/MoviePlanList.vue'),
        meta: {
          nav: [
            { text: '电影院管理' },
            { text: '电影院列表', to: '/home/cinema-list' },
            { text: '放映厅列表' },
            { text: '拍片计划列表' },
          ], // 动态生成导航
        },
      },
      {
        path: '/home/seat-template/:room_id',
        name: '/home/seat-template/:room_id',
        component: () => import('../views/cinema/CinemaRoomSeatTemplate.vue'),
        meta: {
          nav: [
            { text: '首页' },
            { text: '电影院管理' },
            { text: '电影院列表', to: '/home/cinema-list' },
            { text: '放映厅列表' },
            { text: '配置座位模版' },
          ], // 动态生成导航
        },
      },
    ],
  },
  {
    path: '/user/login',
    name: '/user/login',
    component: () => import('../views/user/Login.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

import store from '../store';
// 全局前置导航守卫每当路由跳转到目标组件之前会自动执行箭头函数
router.beforeEach((to, from, next) => {
  if (to.path == '/user/login') {
    next();
  } else {
    if (!store.state.user) {
      router.push('/user/login');
    } else {
      next();
    }
  }
});

export default router;
